﻿using ETA.IService;
using ETA.Web.App_Start;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ETA.Entity;
using ETA.DomainEnum;
using CentaLine.Infrastructure;
using ETA.Dtos;
using ETA.ClientService;

namespace ETA.Web.Controllers
{
    /// <summary>
    /// 实习生相关控制器
    /// </summary>
    [AuthFilter("实习生")]
    public class TraineeController : BaseController
    {

        private static IRegistrationMainFormService _IRegistrationMainFormService { set; get; }
        private static IUserService _IUserService { set; get; }
        private static IRegistrationBasicInfoService _IRegistrationBasicInfoService { set; get; }
        private static IFlowStepService _IFlowStepService { set; get; }
        private static IFlowLogService _IFlowLogService { set; get; }
        private static IRegistrationEducationService _IRegistrationEducationService { set; get; }
        private static IRegistrationFamilayService _IRegistrationFamilayService { set; get; }
        private static IRegistrationJobService _IRegistrationJobService { set; get; }
        private static IWorkTypeService _IWorkTypeService { set; get; }
        private static IMBTIService _IMBTIService { set; get; }

        public TraineeController(IRegistrationMainFormService service, IUserService userService,
            IRegistrationBasicInfoService registrationBasicInfoService, IFlowStepService IFlowStepService,
            IFlowLogService IFlowLogService, IRegistrationEducationService IRegistrationEducationService,
            IRegistrationFamilayService IRegistrationFamilayService, IRegistrationJobService IRegistrationJobService,
            IWorkTypeService IWorkTypeService, IMBTIService IMBTIService
            )
        {
            _IRegistrationMainFormService = service;
            _IUserService = userService;
            _IRegistrationBasicInfoService = registrationBasicInfoService;
            _IFlowStepService = IFlowStepService;
            _IFlowLogService = IFlowLogService;
            _IRegistrationEducationService = IRegistrationEducationService;
            _IRegistrationFamilayService = IRegistrationFamilayService;
            _IRegistrationJobService = IRegistrationJobService;
            _IWorkTypeService = IWorkTypeService;
            _IMBTIService = IMBTIService;
        }


        //
        // GET: /Trainee/

        public ActionResult Index()
        {
            var response = _IRegistrationMainFormService.QueryRegistrationMainFormList(new QueryRegistrationMainFormRequest
            {
                UserId = CurrentUser.UserId
            });
            if (response.Result.IsSuccess && response.RegistrationMainForms.Any())
            {
                if (response.RegistrationMainForms.FirstOrDefault().FlowLogs != null)
                {
                    return View(response.RegistrationMainForms.FirstOrDefault().FlowLogs.OrderBy(f => f.FlowStepInfo.FlowIndex));
                }
                else
                {
                    return View();
                }

            }
            else
            {
                return Content("主流程丢失");
            }
        }

        #region 报名登记表
        /// <summary>
        /// 1注册登记信息完成
        /// </summary>
        /// <returns></returns>
        public ActionResult FillRegistrationForm()
        {
            var basicInfo = new RegistrationBasicInfo();
            var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId);
            if (user != null && user.RegistrationMainForms.Any())
            {
                var mainForms = user.RegistrationMainForms.FirstOrDefault();
                if (mainForms.RegistrationBasicInfos.Any())
                {
                    ViewBag.EducationLevelEnum = ((Enum)EducationLevelEnum.无学历).
                        ToSelectListItem(mainForms.RegistrationBasicInfos.FirstOrDefault().EducationLevelEnum + "");
                    basicInfo = mainForms.RegistrationBasicInfos.FirstOrDefault();
                }
                else
                {
                    ViewBag.EducationLevelEnum = ((Enum)EducationLevelEnum.无学历).
                       ToSelectListItem();

                }
                FillBasicInfo(basicInfo, user);
                ViewBag.WorkTypeList = GetWorkTypeList();
                return View(mainForms);
            }
            else
            {
                return Content("主流程丢失,异常");
            }
        }

        /// <summary>
        /// 个人信息中回填到报名登记表中
        /// </summary>
        /// <param name="basicInfo"></param>
        /// <param name="user"></param>
        private void FillBasicInfo(RegistrationBasicInfo basicInfo, User user)
        {
            if (string.IsNullOrEmpty(basicInfo.Name))
            {
                basicInfo.Name = user.UserName;
            }
            if (basicInfo.Age == null && user.Age != null)
            {
                basicInfo.Age = (byte)user.Age;
            }
            if (user.SexEnum == SexEnum.女)
            {
                basicInfo.SexEnum = user.SexEnum;
            }
            if (user.EducationLevelEnum != EducationLevelEnum.无学历)
            {
                basicInfo.EducationLevelEnum = user.EducationLevelEnum;
            }

            if (string.IsNullOrEmpty(basicInfo.IDCard))
            {
                basicInfo.IDCard = user.IDCard;
            }
            if (string.IsNullOrEmpty(basicInfo.OrginalJobs))
            {
                basicInfo.OrginalJobs = user.OrginalJobs;
            }
            if (string.IsNullOrEmpty(basicInfo.NewJobs))
            {
                basicInfo.NewJobs = user.NewJobs;
            }
            if (string.IsNullOrEmpty(basicInfo.Birthday) && user.Birthday.HasValue)
            {
                basicInfo.Birthday = user.Birthday.Value.ToShortDateString();
            }
            if (string.IsNullOrEmpty(basicInfo.IDCard))
            {
                basicInfo.IDCard = user.IDCard;
            }
            if (string.IsNullOrEmpty(basicInfo.BirthPlace))
            {
                basicInfo.BirthPlace = user.Ahousehold;
            }
        }
        /// <summary>
        /// 所有项目列表
        /// </summary>
        /// <returns></returns>
        public IEnumerable<WorkTypeDto> GetWorkTypeList()
        {
            IEnumerable<WorkTypeDto> workTypeList = null;
            var response = _IWorkTypeService.QueryWorkTypeList(new QueryWorkTypeRequest());
            if (response.Result.IsSuccess)
            {
                workTypeList = response.WorkTypeList;
            }
            return workTypeList;
        }

        [HttpPost]
        public ActionResult SaveProjectInfo(string newJobs)
        {
            var response = new Response();
            if (ModelState.IsValid)
            {
                var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId);
                if (user.RegistrationMainForms.Any())
                {
                    var registrationBasicInfos = user.RegistrationMainForms.FirstOrDefault().RegistrationBasicInfos;
                    if (registrationBasicInfos.Any())//更新
                    {
                        var model = registrationBasicInfos.First();
                        model.NewJobs = newJobs;
                        var modifyRep = _IRegistrationBasicInfoService.ModifyRegistrationBasicInfo(new ModifyRegistrationBasicInfoRequest
                        {
                            RegistrationBasicInfo = model
                        });
                        response = modifyRep;
                    }
                    else //创建
                    {
                        var model = new RegistrationBasicInfo();
                        model.RegistrationBasicInfoId = Guid.NewGuid() + "";
                        model.RegistrationMainFormId = user.RegistrationMainForms.First().RegistrationMainFormId;
                        model.NewJobs = newJobs;
                        var createRep = _IRegistrationBasicInfoService.CreateRegistrationBasicInfo(new CreateRegistrationBasicInfoRequest
                        {
                            RegistrationBasicInfo = model
                        });
                        response = createRep;
                    }
                }
                else
                {
                    response.Result = new Dtos.ResultDto
                    {
                        ErrMessage = "主流程缺失，无法保存",
                        IsSuccess = false
                    };
                }
            }
            return JsonWithDate(response);
        }

        [HttpPost]
        public ActionResult SaveIntroduce(string Character, string Interest, string Advantage, string DisAdvantage)
        {
            var response = new Response();
            if (ModelState.IsValid)
            {
                var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId);
                if (user.RegistrationMainForms.Any())
                {
                    var registrationBasicInfos = user.RegistrationMainForms.FirstOrDefault().RegistrationBasicInfos;
                    if (registrationBasicInfos.Any())//更新
                    {
                        var model = registrationBasicInfos.First();
                        model.Character = Character;
                        model.Interest = Interest;
                        model.Advantage = Advantage;
                        model.DisAdvantage = DisAdvantage;

                        var modifyRep = _IRegistrationBasicInfoService.ModifyRegistrationBasicInfo(new ModifyRegistrationBasicInfoRequest
                        {
                            RegistrationBasicInfo = model
                        });
                        response = modifyRep;
                    }
                    else //创建
                    {
                        var model = new RegistrationBasicInfo();
                        model.RegistrationBasicInfoId = Guid.NewGuid() + "";
                        model.RegistrationMainFormId = user.RegistrationMainForms.First().RegistrationMainFormId;
                        model.Character = Character;
                        model.Interest = Interest;
                        model.Advantage = Advantage;
                        model.DisAdvantage = DisAdvantage;
                        var createRep = _IRegistrationBasicInfoService.CreateRegistrationBasicInfo(new CreateRegistrationBasicInfoRequest
                        {
                            RegistrationBasicInfo = model
                        });
                        response = createRep;
                    }
                }
                else
                {
                    response.Result = new Dtos.ResultDto
                    {
                        ErrMessage = "主流程缺失，无法保存",
                        IsSuccess = false
                    };
                }
            }
            return JsonWithDate(response);
        }


        /// <summary>
        /// 基本信息保存
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveBasicInfo(RegistrationBasicInfo model)
        {
            var response = new Response();
            if (ModelState.IsValid)
            {
                var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId);
                if (user.RegistrationMainForms.Any())
                {
                    var registrationBasicInfos = user.RegistrationMainForms.FirstOrDefault().RegistrationBasicInfos;
                    if (registrationBasicInfos.Any())//更新
                    {
                        model.RegistrationBasicInfoId = registrationBasicInfos.First().RegistrationBasicInfoId;
                        var modifyRep = _IRegistrationBasicInfoService.ModifyRegistrationBasicInfo(new ModifyRegistrationBasicInfoRequest
                        {
                            RegistrationBasicInfo = model
                        });
                        response = modifyRep;
                    }
                    else //创建
                    {
                        model.RegistrationBasicInfoId = Guid.NewGuid() + "";
                        model.RegistrationMainFormId = user.RegistrationMainForms.First().RegistrationMainFormId;
                        var createRep = _IRegistrationBasicInfoService.CreateRegistrationBasicInfo(new CreateRegistrationBasicInfoRequest
                        {
                            RegistrationBasicInfo = model
                        });
                        response = createRep;
                    }
                }
                else
                {
                    response.Result = new Dtos.ResultDto
                    {
                        ErrMessage = "主流程缺失，无法保存",
                        IsSuccess = false
                    };
                }
            }
            return JsonWithDate(response);
        }

        /// <summary>
        /// 学历
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveEducation(RegistrationEducation model)
        {
            var response = new Response();
            if (ModelState.IsValid)
            {
                var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId); ;
                if (user.RegistrationMainForms.Any())
                {
                    if (!string.IsNullOrEmpty(model.RegistrationEducationId))//更新
                    {
                        model.RegistrationMainFormId = user.RegistrationMainForms.First().RegistrationMainFormId;
                        var modifyRep = _IRegistrationEducationService.ModifyRegistrationEducation(new ModifyRegistrationEducationRequest
                        {
                            RegistrationEducation = model
                        });
                        response = modifyRep;
                    }
                    else //创建
                    {
                        model.RegistrationEducationId = Guid.NewGuid() + "";
                        model.RegistrationMainFormId = user.RegistrationMainForms.First().RegistrationMainFormId;
                        var createRep = _IRegistrationEducationService.CreateRegistrationEducation(new CreateRegistrationEducationRequest
                        {
                            RegistrationEducation = model
                        });
                        response = createRep;
                    }
                }
                else
                {
                    response.Result = new Dtos.ResultDto
                    {
                        ErrMessage = "主流程缺失，无法保存",
                        IsSuccess = false
                    };
                }
            }
            return JsonWithDate(response);
        }

        /// <summary>
        /// 工作经历
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveJob(RegistrationJob model)
        {
            var response = new Response();
            if (ModelState.IsValid)
            {
                var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId); ;
                if (user.RegistrationMainForms.Any())
                {
                    if (!string.IsNullOrEmpty(model.RegistrationJobId))//更新
                    {
                        model.RegistrationMainFormId = user.RegistrationMainForms.First().RegistrationMainFormId;
                        var modifyRep = _IRegistrationJobService.ModifyRegistrationJob(new ModifyRegistrationJobRequest
                        {
                            RegistrationJob = model
                        });
                        response = modifyRep;
                    }
                    else //创建
                    {
                        model.RegistrationJobId = Guid.NewGuid() + "";
                        model.RegistrationMainFormId = user.RegistrationMainForms.First().RegistrationMainFormId;
                        var createRep = _IRegistrationJobService.CreateRegistrationJob(new CreateRegistrationJobRequest
                        {
                            RegistrationJob = model
                        });
                        response = createRep;
                    }
                }
                else
                {
                    response.Result = new Dtos.ResultDto
                    {
                        ErrMessage = "主流程缺失，无法保存",
                        IsSuccess = false
                    };
                }
            }
            return JsonWithDate(response);
        }

        /// <summary>
        /// 家庭成员
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult SaveFamilay(RegistrationFamilay model)
        {
            var response = new Response();
            if (ModelState.IsValid)
            {
                var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId); ;
                if (user.RegistrationMainForms.Any())
                {
                    if (!string.IsNullOrEmpty(model.RegistrationFamilayId))//更新
                    {
                        model.RegistrationMainFormId = user.RegistrationMainForms.First().RegistrationMainFormId;
                        var modifyRep = _IRegistrationFamilayService.ModifyRegistrationFamilay(new ModifyRegistrationFamilayRequest
                        {
                            RegistrationFamilay = model
                        });
                        response = modifyRep;
                    }
                    else //创建
                    {
                        model.RegistrationFamilayId = Guid.NewGuid() + "";
                        model.RegistrationMainFormId = user.RegistrationMainForms.First().RegistrationMainFormId;
                        var createRep = _IRegistrationFamilayService.CreateRegistrationFamilay(new CreateRegistrationFamilayRequest
                        {
                            RegistrationFamilay = model
                        });
                        response = createRep;
                    }
                }
                else
                {
                    response.Result = new Dtos.ResultDto
                    {
                        ErrMessage = "主流程缺失，无法保存",
                        IsSuccess = false
                    };
                }
            }
            return JsonWithDate(response);
        }


        /// <summary>
        /// 提交报名登记表审核
        /// </summary>
        /// <returns></returns>
        public ActionResult SumbitRegistrationForm()
        {
            var res = RegistrationMainFormBll.SubmitFlowLog(CurrentUser.UserId, 1, FlowLogStateEnum.进行中);
            return Json(new Response
            {
                Result = new ResultDto
                {
                    IsSuccess = res.Result.IsSuccess,
                    ErrMessage = res.Result.ErrMessage,
                    Message = res.Result.Message
                }
            });
        }

        #endregion 报名登记表

        /// <summary>
        /// 2网上评定
        /// </summary>
        /// <returns></returns>
        public ActionResult OnlineTest()
        {
            var user = UserBll.GetCompltetUserInfo(CurrentUser.UserId);
            return View(user);
        }

        /// <summary>
        /// 3签订《参加直通车面试承诺书》
        /// </summary>
        /// <returns></returns>
        public ActionResult CommitmentSign()
        {
            return View();
        }

        /// <summary>
        /// 4缴纳报名定金
        /// </summary>
        /// <returns></returns>
        public ActionResult PayDeposit()
        {
            return View();
        }

        /// <summary>
        ///5出国费用缴纳
        /// </summary>
        /// <returns></returns>
        public ActionResult PayGoabroadFee()
        {
            return View();
        }
    }
}
